package com.itheima.tanhua.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.itheima.tanhua.api.db.UserInfoServiceApi;
import com.itheima.tanhua.api.mongo.RecommendServiceApi;
import com.itheima.tanhuan.pojo.db.UserInfo;
import com.itheima.tanhuan.pojo.mongo.RecommendUser;
import com.itheima.tanhuan.vo.mongo.PageResult;
import com.itheima.tanhuan.vo.mongo.TodayBestVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class RecommendService {

    @Autowired
    private RestTemplate restTemplate;
    @DubboReference
    private RecommendServiceApi recommendServiceApi;
    @DubboReference
    private UserInfoServiceApi userInfoServiceApi;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 1、今日佳人
     * @return
     */
    public TodayBestVo todayBest() {
       /* //1、校验token
        String url="http://tanhua-sso/user/"+token;
        //2、当前登陆者id
        Long uid = restTemplate.getForObject(url, Long.class);*/
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get("UID"));
        //3、用uid到mongo中查询缘分值最高的用户
        RecommendUser recommendUser = recommendServiceApi.todayBest(uid);
        //4、获取对应佳人userId到mysql数据库中获取对应的信息
        UserInfo userInfo=userInfoServiceApi.findById(recommendUser.getUserId());
        TodayBestVo todayBestVo=new TodayBestVo();
        //mongo中的缘分值，mysql中当前佳人的用户信息
        //使用工具做对象属性值得copy
        BeanUtil.copyProperties(userInfo,todayBestVo);
        //处理tags
        todayBestVo.setTags(StrUtil.split(userInfo.getTags(),","));
        todayBestVo.setFateValue(Convert.toInt(recommendUser.getScore()));
        return todayBestVo;
    }

    /**
     * 2、佳人推荐列表
     * @param token
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult recommendation(String token, Integer page, Integer pagesize) {
        //1、校验token
        String url="http://tanhua-sso/user/"+token;
        //2、当前登陆者id
        Long uid = restTemplate.getForObject(url, Long.class);
        //3、用uid到mongo中查询所有佳人信息
        List<RecommendUser> recommendUserList=recommendServiceApi.findByToUid(uid);
       /* //4、从recommendUserList获取所有userId
        List<Object> userIds = CollUtil.getFieldValues(recommendUserList, "userId");
        //5、根据userIds查询所有的用户信息*/
        List<Object> list=new ArrayList<>();
        //4、遍历recommendUserList   stream
        for (RecommendUser recommendUser : recommendUserList) {
            TodayBestVo todayBestVo=new TodayBestVo();
            Long userId = recommendUser.getUserId();
            UserInfo userInfo = userInfoServiceApi.findById(userId);
            //使用工具做对象属性值得copy
            BeanUtil.copyProperties(userInfo,todayBestVo);
            //处理tags
            todayBestVo.setTags(StrUtil.split(userInfo.getTags(),","));
            todayBestVo.setFateValue(Convert.toInt(recommendUser.getScore()));
            list.add(todayBestVo);
        }

        return new PageResult(page,pagesize,list);
    }
}
